import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
// import { getBaseUrl } from '@/services/urlconst';
import { commonManageSearchParams, getLoadMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';
import { getManageTabSwitch, getSearchList } from '../mconfig';
import { getBaseUrl } from '@/services/urlconst';
const baseUrl = getBaseUrl();
// const baseUrl = 'http://192.168.153.246:31019'; //旭潮本地
// const baseUrl = 'KLapi';

const warnManageService = (curRes: Record<string, any>) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.ASSET_EQUITY}`,
    `${getTenantID()}${ILoadMapTypeAll.ASSET_STATUS}`,
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_TYPE}`,
  ];

  /** 获取数据字典loadMdicMap */
  const loadMdicMapObj: Record<string, any> = getLoadMapList(loadMdicIds);

  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = getManageTabSwitch(curRes);

  /** 获取搜索参数 */
  const searchListParams = getSearchList(loadMdicMapObj, queryTypeObj);

  /** 获取列表数据  */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
        current: 1,
        pageSize: 10,
        // sort: 'readTime',
        // order: 'desc',
        // dicTypeIds: loadMdicIds?.toString(),
        ...opt,
      };

      const searchParams: Record<string, any> = commonManageSearchParams(searchListParams, requestBody, {
        // dicTransName: 'orderType',
        // orgTransName: `${ISearchTranslateAll.OWNER_DEPT}`,
        // areaTransName: `${ISearchTranslateAll.AREA_TRANS}`,
      });
      const requestURL = `${baseUrl}/assetInfo/list/${queryTypeObj.params}`;
      return requestFunComponFiex('POST', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getWarnData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    if (request?.createTime) {
      const [createTimeStart, createTimeEnd] = request.createTime;
      request['createTimeStart'] = createTimeStart.format('YYYY-MM-DD 00:00:00');
      request['createTimeEnd'] = createTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['createTime'];
    }

    delete request['userDepartmentFormNameDept'];
    delete request['userDepartmentFormNameUser'];
    if (request?.userID && request?.userID?.length > 0) {
      request['userID'] = request?.userID?.toString();
    } else {
      delete request['userID'];
    }

    run({
      ...request,
    });
  };

  /** 搜索列表  */
  const searchList = useMemo(() => {
    return {
      searchList: searchListParams,
    };
  }, [loadMdicMapObj?.loadMapInfo]);

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getWarnData,
    reloadRefresh: refresh,
    ...returnTableProps,
    ...searchList,
  };
};

const wareaddService = (params, userInfo) => {
  const obj = params.data;
  obj.pId = null;
  // obj.createTime = '2024-01-04'
  obj.createrID = userInfo?.createrID;
  obj.createPositionID = userInfo?.createPositionID;
  obj.createDepartmentID = userInfo?.createDepartmentID;
  obj.createCompanyID = userInfo?.createCompanyID;
  /** 获取列表数据  */
  // const valueData = useRequest(
  //   () => {
  const requestURL = `${baseUrl}/assetStore/add`;
  return requestFunComponFiex('POST', requestURL, obj);
  //   },
  // );

  // const data = useMemo(() => {
  //   return valueData?.data;
  // }, [valueData?.data]);

  // return {
  //   valueData: data,
  // };
  // console.log('保存成功',useRequest)
};

const wareditService = (params, userInfo) => {
  const obj = params.data;
  obj.modifyerID = userInfo?.modifyerID;
  obj.modifyPositionID = userInfo?.modifyPositionID;
  obj.modifyDepartmentID = userInfo?.modifyDepartmentID;
  obj.modifyCompanyID = userInfo?.modifyCompanyID;
  const requestURL = `${baseUrl}/assetStore/edit`;
  return requestFunComponFiex('PUT', requestURL, obj);
};

const warcloseService = (params) => {
  const obj = {
    id: params.id,
  };
  const requestURL = `${baseUrl}/assetStore/close`;
  return requestFunComponFiex('PUT', requestURL, obj);
};

const waropenService = (params) => {
  const obj = {
    id: params.id,
    pId: params.pId,
  };
  const requestURL = `${baseUrl}/assetStore/open`;
  return requestFunComponFiex('PUT', requestURL, obj);
};

export { warnManageService, wareaddService, wareditService, warcloseService, waropenService };
